package com.zhuojy.equity.mall.admin.service.salary;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuojy.equity.mall.admin.common.entity.TPosition;
import com.zhuojy.equity.mall.admin.common.entity.bo.salary.TUserArchivesBO;
import com.zhuojy.equity.mall.admin.common.entity.po.salary.TUserArchivesPO;
import com.zhuojy.equity.mall.admin.common.entity.vo.salary.TUserArchivesVO;
import com.zhuojy.equity.mall.admin.dal.mapper.salary.TUserArchivesMapper;
import com.zhuojy.equity.mall.admin.service.PositionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
/**
 * @description: TODO
 * @author chen
 * @date 2024/4/11 18:50
 * @version 1.0
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class TUserArchivesService extends ServiceImpl<TUserArchivesMapper, TUserArchivesPO> {
    private final PositionService positionService;

    public Map getUserArchiveList(TUserArchivesBO tUserArchivesBO) {
        IPage<TUserArchivesPO> page = new Page<>(tUserArchivesBO.getCurrentPage(),tUserArchivesBO.getPageSize());

        LambdaQueryWrapper<TUserArchivesPO> lqw = Wrappers.lambdaQuery();

        if(StringUtils.isNotEmpty(tUserArchivesBO.getName())){
            lqw.like(TUserArchivesPO::getName,tUserArchivesBO.getName());
        }

        page =page(page,lqw);

        List<TUserArchivesPO> tUserArchivesPOS = page.getRecords();

        List<TUserArchivesVO> collect = tUserArchivesPOS.stream().map(tUserArchivesPO -> {
            TUserArchivesVO tUserArchivesVO = new TUserArchivesVO();
            BeanUtils.copyProperties(tUserArchivesPO, tUserArchivesVO);
            TPosition one = positionService.getOne(Wrappers.lambdaQuery(TPosition.class).eq(TPosition::getId, tUserArchivesPO.getPositionId()));
            tUserArchivesVO.setDeptName(one.getDeptName());
            tUserArchivesVO.setPositionName(one.getPositionName());
            return tUserArchivesVO;
        }).collect(Collectors.toList());

        Map map = new HashMap<>();

        map.put("rows",collect);
        map.put("total",page.getTotal());

        return map;

    }

    public List<TUserArchivesVO> exprotPoi() {

        List<TUserArchivesPO> list = list();

        List<TUserArchivesVO> collect = list.stream().map(tUserArchivesPO -> {
            TUserArchivesVO tUserArchivesVO = new TUserArchivesVO();
            BeanUtils.copyProperties(tUserArchivesPO, tUserArchivesVO);
            TPosition one = positionService.getOne(Wrappers.lambdaQuery(TPosition.class).eq(TPosition::getId, tUserArchivesPO.getPositionId()));
            tUserArchivesVO.setDeptName(one.getDeptName());
            tUserArchivesVO.setPositionName(one.getPositionName());
            return tUserArchivesVO;
        }).collect(Collectors.toList());

        return collect;
    }
}
